<?php

/*
 * @Description: 成本
 * @Author: QinTianyu
 * @Date: 2020-04-28 11:18:00
 * @LastEditors: QinTianyu
 * @LastEditTime: 2020-05-07 15:03:16
 */
namespace app\finance\admin;
use app\warehouse\model\Stock as StockModel;
use app\warehouse\model\Warehouse as WarehouseModel;
use app\product\model\Product as ProductModel;
use app\user\model\User as UserModel;
use app\system\admin\Admin;
use think\Db;
class Chengben extends Admin
{

    protected function initialize()
    {
        parent::initialize();
        $users = UserModel::field('id,nick')->all();
        $this->assign('users', $users);
        $products = ProductModel::field('id,name')->all();
        $this->assign('products', $products);
        $wares = WarehouseModel::field('id,name')->all();
        $this->assign('wares', $wares);
        // 库存单明细类型
        // $StockModel = new StockModel;
        // $detailed_types = $StockModel->detailedTypes();
        // $this->assign('detailed_types', $detailed_types);
    }
    public function project()
    {
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);

            $user_id      = $this->request->param('user_id/d', '');
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            $type      = $this->request->param('type', 1);
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!empty($product_id)){
                $where['product_id'] = $product_id;
            }
            if(!empty($warehouse_id)){
                $where['warehouse_id'] = $warehouse_id;
            }
            $where['detailed_type'] = 1;

            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }

            $where['status'] = 1;
            // $where['shop_id'] = session('shop_id');

            $data['data'] = StockModel::where($where)
                            ->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
                            ->whereTime('createtime', [strtotime($start), strtotime($end)])
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }
    public function product()
    {
        // $stockModel = new StockModel;
        if ($this->request->isAjax()) {
            $where      = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);

            $user_id      = $this->request->param('user_id/d', '');
            $product_id      = $this->request->param('product_id/d', '');
            $warehouse_id      = $this->request->param('warehouse_id/d', '');
            $start      = $this->request->param('start', '2000-01-01');
            $end      = $this->request->param('end', date('Y-m-d H:i:s',time()));
            $type      = $this->request->param('type', 1);
            if(!empty($user_id)){
                $where['user_id'] = $user_id;
            }
            if(!empty($product_id)){
                $where['product_id'] = $product_id;
            }
            if(!empty($warehouse_id)){
                $where['warehouse_id'] = $warehouse_id;
            }
            $where['detailed_type'] = 2;

            if(empty($start)){
                $start = '2000-01-01';
            }
            if(empty($end)){
                $end = date('Y-m-d H:i:s',time());
            }


            $where['status'] = 1;
            // $where['shop_id'] = session('shop_id');

            $data['data'] = StockModel::where($where)
                            ->field('*, detailed_type as detailed_type_text,user_id as user_name,warehouse_id as warehouse_text')
                            ->whereTime('createtime', [strtotime($start), strtotime($end)])
                            ->page($page)
                            ->limit($limit)
                            ->order('id desc')
                            ->select();
            $data['count'] = StockModel::where($where)->whereTime('createtime', [strtotime($start), strtotime($end)])->count('id');
            $data['code'] = 0;
            return json($data);

        }

        return $this->fetch();
    }
}
